package com.freedom.start.sys.controller;

import com.freedom.start.common.Result;
import com.freedom.start.sys.entity.UserEntity;
import com.freedom.start.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * @Author: zhenggang.liu
 * @Date: 2020/2/10 16:51
 */
@Controller
@RequestMapping("/sys/user")
public class UserController {

    @Autowired
    UserService userService;

    @ModelAttribute
    public UserEntity get(String id) {
        if (StringUtils.isNotBlank(id)) {
            return userService.get(id);
        }
        return new UserEntity();
    }

    /**
     * 返回list页面
     *
     * @return
     */
    @RequestMapping("/list")
    public String list() {
        return "sys/user/list";
    }

    /**
     * 返回增加或是修改页面
     *
     * @return
     */
    @RequestMapping("/form")
    public String form() {
        return "sys/user/form";
    }

    /**
     * 保存
     *
     * @param userEntity 前台传入的属性封装到此对象中
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public Result save(@Valid UserEntity userEntity){
        try {
            userService.save(userEntity);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success("success");
    }

    /**
     * 返回json对象，供easyui使用
     *
     * @param request   这里从中获取分页信息
     * @param userEntity 封装搜索条件
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/dataList")
    public Map<String, Object> dataList(HttpServletRequest request, UserEntity userEntity) {
        Map<String, Object> pageForEasyUI = userService.findPageForEasyUI(request, userEntity);
        return pageForEasyUI;
    }

    /**
     * 删除，支持批量
     *
     * @param ids 记录的id，多条用“,”分割
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete")
    public Result delete(String ids) {
        try {
            userService.deleteBatch(ids);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success("删除成功");
    }

    @ResponseBody
    @RequestMapping(value = "/resetPassword")
    public Result resetPassword(UserEntity userEntity){
        userEntity.setPassword("123");
        try {
            userService.save(userEntity);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success("用户密码重置成功！");
    }

    /**
     * 返回用户信息，用户修改自身信息
     * @param userEntity
     * @return
     */
    @RequestMapping(value = "/userInfo")
    public String userInfo(UserEntity userEntity){
        return "/sys/user/userInfo";
    }
}
